/*
 Engine Template
 for designing interactive system with Qt

 Chad McKinney 2011
 seppukuzombie@gmail.com
 chadmckinneyaudio.com

 GUI elements should all be constructed with the Model View Controller paradigm.?!?!?

 TO DO:

    1. WORKING ON ThreadSafeQList, a QList that has resource locking....IT WORKS! Add more functionality?

    2. Flesh out EnBody. Add QImages to it...and drawableness???

    3. Flesh out EnModule. Add more relevant content. Create an EnModule loader, that switches between EnModules. Create
    a save state function that saves the current module's state to a file.

    4. Create a QSettings system that saves the basic settings of the sessions so that the program reopens with the same
    settings.

    5. Don't forget to use QConcurrent::run and such for one off methods like creating monsters or saving and loading modules
    or settings.


 NEW IDEA:

    Don't multi thread QGraphicsView/QGraphicsScene and peripheral detection. use timers ( at the end  of update loops ) with
    independant timing. However redesign EnTimedThread to use the correct .moveToThread( &threadInstance ) approach for other
    things that DO need to be multithreaded. Signals/Slots can be used for inter thread connectivity. Peripheral detection is
    low overhead so I think having it on the same thread, but with asynchronous cycles is a fine solution.

    *** Rembember ***
    Draw order is based on Z value of the QGraphicsItem

    QGraphicsView updates itself! No frame rate needed.

    DONE:
    Add a selection rect for selecting multiple items.

    FINALLY TO DO:

    0. Multiple selection isn't working!??!
    1. Add ellipse deletion.
    2. Finish networking.

    Possibly later...
    3. Investigate efficiency improvement.
    4. Create a Module saver/loader based on XML?

 */

#include <QtGui/QApplication>
#include <enlogic.h>
#include <QSemaphore>
#include <lockedsemaphore.h>
#include <ThreadSafeQList.h>
#include <QDebug>
#include <enbody.h>
#include <enmodule.h>
#include <endrawcontext.h>
#include <demomenu.h>
#include <OSCthulhuClient.h>
#include <enparseosc.h>

int main(int argc, char *argv[])
{
    // Hangs until we clean up all our threads and such
    //QSemaphore* waitForCleanUp = new QSemaphore( 1 );
    LockedSemaphore* waitForCleanUp = new LockedSemaphore( 1 );
    int r;
    QApplication a(argc, argv);

    OSCthulhuClient* client = new OSCthulhuClient(
                QString(argv[2]), // serverAddress
                QString(argv[3]), // serverPassword
                QString(argv[4]), // userName
                QString(argv[5]), // userPassword
                atoi(argv[6]), // serverPort
                atoi(argv[7]), // clientPort
                atoi(argv[8]), // appPort
                waitForCleanUp ); // wait4CleanupDone

    QObject::connect( &a, SIGNAL( aboutToQuit() ), client, SLOT( handleQuit() ) );

    EnModule* currentModule = new EnModule( argv[4] );
    currentModule->setBackgroundFilename( ":/images/clouds.jpg" );

    EnDrawContext drawContext;
    drawContext.loadModule( currentModule );

    EnParseOSC* parseOSC = new EnParseOSC( client, currentModule );

    DemoMenu demoMenu( 0, &drawContext );

    /*
    EnLogic logic( 30, waitForCleanUp, a );
    logic.start();
    logic.setPriority( QThread::HighPriority );
    */

    r = a.exec();

    // Clean Up
    waitForCleanUp->acquire( 1 );
    delete waitForCleanUp;
    // free the argument list from memory
    delete currentModule;
    delete parseOSC;
    return r;
}
